import requests
api_url='https://api.github.com/search/repositories?q=spider'
req=requests.get(api_url)
print('状态码:',req.status_code)
req_dic=req.json()
print(req_dic.keys())


import requests
api_url='https://api.github.com/search/repositories?q=spider'
req=requests.get(api_url)
print('状态码:',req.status_code)
req_dic=req.json()
print('与spider有关的库总数:',req_dic['total_count'])
print('本次请求是否完整:',req_dic['incomplete_results'])
req_dic_items=req_dic['items']
print('当前页面返回的项目数量:',len(req_dic_items))
req_dic_items_first=req_dic_items[0]
print('查看第一个项目中的键数量:',len(req_dic_items_first))
print('第一个项目中的具体内容:',req_dic_items_first)
print('获得第一个项目作者的登录名:',req_dic_items_first['owner']['login'])
print('获得第一个项目全名:',req_dic_items_first['full_name'])
print('获得第一个项目的描述:',req_dic_items_first['description'])
print('获得第一个项目评分:',req_dic_items_first['score'])




import requests
import pymysql

api_url='https://api.github.com/search/repositories?q=spider'
req=requests.get(api_url)
print('状态码:',req.status_code)
req_dic=req.json()
print('与spider有关的库总数:',req_dic['total_count'])
print('本次请求是否完成:',req_dic['incomplete_results'])
req_dic_items=req_dic['items']
print('当前页面返回的项目数量:',len(req_dic_items))
db = pymysql.connect(host='localhost', user='root', password='123456', port=3306)
cursor=db.cursor()
cursor.execute("CREATE DATABASE WEBAPI3 DEFAULT CHARACTER SET utf8mb4")
db.close()
db2 = pymysql.connect(host='localhost', user='root', password='123456', port=3306, database='WEBAPI3')
cursor2=db2.cursor()
cursor2.execute("DROP TABLE IF EXISTS webapi3")
sql1="""CREATE TABLE `webapi3`(
                `id`int(10) NOT NULL AUTO_INCREMENT,
                `full_name`char(20) NOT NULL,
                `score` int(10) NOT NULL,
                PRIMARY KEY(`id`)
                )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"""
cursor2.execute(sql1)
print("Created table Successfull.")
for index,key in enumerate(req_dic_items):
    print('项目序号:',index,'项目名称:',key['full_name'],'项目评分:',key['score'])
sql2='INSERT INTO wenapi3(id,full_name,score) VALUES(%s,%s,%s)'
try:
    cursor2.execute(sql2,(index,key['full_name'],key['score']))
    db2.commit()
except:
    db2.rollback()
db2.close()